package com.leilei;

import org.apache.flink.api.common.RuntimeExecutionMode;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.datastream.WindowedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.windowing.RichWindowFunction;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.TumblingProcessingTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.GlobalWindow;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.streaming.api.windowing.windows.Window;
import org.apache.flink.util.Collector;

/**
 * @author lei
 * @version 1.0
 * @date 2021/3/17 22:56
 * @flink manageState下 keyState 下的 valueState
 */
public class Flink_State_1_ValueState {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setRuntimeMode(RuntimeExecutionMode.BATCH);
        env.setParallelism(1);
        DataStreamSource<Tuple2<String, Integer>> source = env.fromElements(
                Tuple2.of("迪迦奥特曼", 45),
                Tuple2.of("赛文奥特曼", 45),
                Tuple2.of("迪迦奥特曼", 24),
                Tuple2.of("赛文奥特曼", 99),
                Tuple2.of("赛文奥特曼", 98),
                Tuple2.of("雷欧奥特曼", 21));
        SingleOutputStreamOperator<Tuple2<String, Integer>> stream = source.keyBy(t -> t.f0).maxBy(1);
        stream.print("maxBy算子 底层自动实现");
        SingleOutputStreamOperator<Tuple2<String, Integer>> result = source.keyBy(t -> t.f0)
                .window(TumblingProcessingTimeWindows.of(Time.seconds(5)))
                .apply(new MyCalcWindow());
        result.print("valueState 个人手动实现");
        env.execute();
    }

    public static class MyCalcWindow extends RichWindowFunction<Tuple2<String, Integer>, Tuple2<String, Integer>, String, TimeWindow> {
        private ValueState<Integer> maxHeightState = null;

        @Override
        public void apply(String s, TimeWindow window, Iterable<Tuple2<String, Integer>> input, Collector<Tuple2<String, Integer>> out) throws Exception {

            for (Tuple2<String, Integer> tuple2 : input) {
                // 缓存中获取历史身高记录
                Integer historyHeight = maxHeightState.value();
                // 输入数据中获取当前身高记录
                Integer currentHeight = tuple2.f1;
                if (historyHeight == null || historyHeight < currentHeight) {
                    historyHeight = currentHeight;
                    maxHeightState.update(historyHeight);
                }
            }
            out.collect(Tuple2.of(s,maxHeightState.value()));
        }

        @Override
        public void open(Configuration parameters) throws Exception {
            super.open(parameters);
            ValueStateDescriptor<Integer> heightStateDescriptor = new ValueStateDescriptor<>("maxHeightState", Integer.class);
            // 初始化身高缓存
            maxHeightState = getRuntimeContext().getState(heightStateDescriptor);
        }
    }

}
